package com.css.fxfzysx.modules.ysxManagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vaolEngineering.repository.ValEngineeringRepository;
import com.css.fxfzysx.modules.ysxManagement.entity.DynamicFieldsEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.RsRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.WsfCoefficientEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.ZqtEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.WsfCoefficientRepository;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.modules.ysxManagement.service.WsfCoefficientService;
import com.css.fxfzysx.modules.ysxManagement.service.ZqtCiService;
import com.css.fxfzysx.modules.ysxManagement.vo.RsRatioVo;
import com.css.fxfzysx.modules.ysxManagement.vo.WsfCoefficientVo;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class WsfCoefficientServiceImpl implements WsfCoefficientService {

    @Autowired
    private WsfCoefficientRepository wsfCoefficientRepository;

    @Autowired
    private ZqtCiService zqtCiService;

    @Autowired
    private ZqtCiRepository zqtCiRepository;

    @Autowired
    private ValEngineeringRepository valEngineeringRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveOrUpdateWsfCoefficient(WsfCoefficientVo wsfCoefficientVo) {
        try {
            List<WsfCoefficientEntity> wsfCoefficientList = wsfCoefficientVo.getWsfCoefficientList();
            List<DynamicFieldsEntity> dynamicFieldsList = wsfCoefficientVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(wsfCoefficientList)) {
                wsfCoefficientList.stream().forEach(it -> {
                    if(PlatformObjectUtils.isEmpty(it.getCwdfId())){
                        it.setCwdfId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        wsfCoefficientRepository.saveWsfCoefficient(it);
                    }else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        wsfCoefficientRepository.updateWsfCoefficient(it);
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllWsfCoefficient(String titleName) {
        try {
            WsfCoefficientVo wsfCoefficientVo = new WsfCoefficientVo();
            List<WsfCoefficientEntity> wsfCoefficientList = wsfCoefficientRepository.getAllWsfCoefficient();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            wsfCoefficientVo.setWsfCoefficientList(wsfCoefficientList);
            wsfCoefficientVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(wsfCoefficientVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteWsfCoefficientById(String wsfCoefficientIds,String arrangeIds) {
        try {

            if(PlatformObjectUtils.isNotEmpty(wsfCoefficientIds)){
                for (String wsfCoefficientId : wsfCoefficientIds.split(",")) {
                     wsfCoefficientRepository.deleteWsfCoefficientById(wsfCoefficientId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    wsfCoefficientRepository.deleteWsfCoefficientByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse saveOrUpdateRsRatio(RsRatioVo rsRatioVo) {
        try {
            List<RsRatioEntity> rsRatioList = rsRatioVo.getRsRatioList();
            List<DynamicFieldsEntity> dynamicFieldsList = rsRatioVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(rsRatioList)) {
                rsRatioList.forEach(it -> {
                    if(PlatformObjectUtils.isEmpty(it.getCrsrId())){
                        it.setCrsrId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        wsfCoefficientRepository.saveRsRatio(it);
                    }else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        List<RsRatioEntity> list = wsfCoefficientRepository.getRsRatioById(it.getCrsrId());
                        list.forEach(rs->{
                            if (rs.getRebarValue()==null || !rs.getRebarValue().equals(it.getRebarValue()) ){
                                valEngineeringRepository.updateCountStatusByModelIds1Id(it.getCrsrId());
                            }
                        });
                        wsfCoefficientRepository.updateRsRatio(it);
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllRsRatio(String titleName) {
        try {
            RsRatioVo rsRatioVo = new RsRatioVo();
            List<RsRatioEntity> rsRatioList = wsfCoefficientRepository.getAllRsRatio();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            rsRatioVo.setRsRatioList(rsRatioList);
            rsRatioVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(rsRatioVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteRsRatioById(String rsRatioIds,String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(rsRatioIds)){
                for (String rsRatioId : rsRatioIds.split(",")) {
                    wsfCoefficientRepository.deleteRsRatioById(rsRatioId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    wsfCoefficientRepository.deleteRsRatioByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
